#include "SpinLock.h"
#include "Util.h"

namespace qy
{

SpinLock *SpinLock::create()
{
	pthread_spinlock_t s;
	//pthread_spin_init(pthread_spinlock_t *lock, int pshared);
	if(pthread_spin_init(&s, 0) == 0)
	{
		SpinLock *lock = new SpinLock(s);
		return lock;
	}
	return NULL;
}

void SpinLock::destroy(SpinLock *s)
{
	delete_null(s);
}

void SpinLock::lock()
{
	pthread_spin_lock(&m_spinlock);
}

bool SpinLock::trylock()
{
	return 0 == pthread_spin_trylock(&m_spinlock);
}

void SpinLock::unlock()
{
	pthread_spin_unlock(&m_spinlock);
}

SpinLock::SpinLock(pthread_spinlock_t s)
:m_spinlock(s)
{
}

SpinLock::~SpinLock()
{
	pthread_spin_destroy(&m_spinlock);
}

}

